package com.newbookstore.controller;

import com.newbookstore.Model.Bookstore;
import com.newbookstore.service.BookstoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class BookstoreController {
    @Autowired
    private BookstoreService bookstoreService;

//    @GetMapping("/")
//    public String viewHomepage(Model model) {
//        model.addAttribute("listBookstore", bookstoreService.getAllBookstore());
//        return "index";
//    }
    @GetMapping("/")
    public String viewHomePage(Model model) {
    return findPaginated(1, "name", "asc", model);
}

    @GetMapping("/showNewBookstoreForm")
    public String showNewBookstoreForm(Model model) {
        Bookstore bookstore = new Bookstore();
        model.addAttribute("bookstore", bookstore);
        return "new_book";
    }

    @PostMapping("/saveBookstore")
    public String saveBooks(@ModelAttribute("books") Bookstore bookstore) {
        bookstoreService.saveBookstore(bookstore);
        return "redirect:/";
    }

    @GetMapping("/showFormForUpdate/{id}")
    public String showFormForUpdate(@PathVariable(value = "id") long id, Model model) {
        Bookstore books = bookstoreService.getBookstoreById(id);
        model.addAttribute("bookstore", books);
        return "update_book";
    }

    @GetMapping("/deleteBookstore/{id}")
    public String deleteBookstore(@PathVariable(value = "id") long id) {
        this.bookstoreService.deleteBookstoreById(id);
        return "redirect:/";
    }

    @GetMapping("/query{newbookname}")
    public String query(@PathVariable(value = "newbookname") String newbookname, Model model) {
        List<Bookstore> listBooknumber = bookstoreService.findByBookname(newbookname);
        model.addAttribute("listBookstore", listBooknumber);
        return "index";
    }

    //获取分页数据
    @GetMapping("/page/{pageNo}")
    public String findPaginated(@PathVariable(value = "pageNo") int pageNo,
                                @RequestParam("sortField") String sortField,
                                @RequestParam("sortDir") String sortDir,
                                Model model) {
        int pageSize = 5;

        Page<Bookstore> page = bookstoreService.findPaginated(pageNo, pageSize, sortField, sortDir);
        List<Bookstore> listBookstore = page.getContent();

        model.addAttribute("currentPage", pageNo);
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("totalItems", page.getTotalElements());

        model.addAttribute("sortField", sortField);
        model.addAttribute("sortDir", sortDir);
        model.addAttribute("reverseSortDir", sortDir.equals("asc") ? "desc" : "asc");

        model.addAttribute("listBookstore", listBookstore);
        return "index";
    }
}

